<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
</head>
<body>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
<script type="text/javascript">
	// 浅拷贝:当b复制了a ,b的儿子属性不会随a的变化而变化,但是从第二层开始至深都会开始改变


	let a = {
		name:'孙悟空',
		age : 20,
		car:['筋斗云','BWM']
	}

	//实现浅拷贝
	// 1.for循环
	/*let b = {}

	function copy(arr1,arr2) {
		for(let key in arr1){
			console.log(key)
			arr2[key] = arr1[key]
		}
	}
	copy(a,b)
	a.name  = '猪八戒'
	a.car[0] = '奔跑'
	console.log(a)
	console.log(b)*/

	// 2.Object.assign(目标对象,源对象)
	/*let b = {}
	Object.assign(b,a)
	a.name  = '猪八戒'
	a.car[0] = '奔跑'
	console.log(a)
	console.log(b)
*/

	//3.slice()
	let arr = [1,2,[3,4]]
	let b = []
/*	b = arr.slice()
	arr[0] = 66
	arr[2][0] = 88
	console.log(arr,b)*/

	//4.concat()

	// b = b.concat(arr)
	// arr[0] = 66
	// arr[2][0] = 88
	// console.log(arr,b)

	//5.es6的剩余运算符 ...
	// b = [...arr]
	// arr[0] = 66
	// arr[2][0] = 88
	// console.log(arr,b)

	//2.深拷贝 b复制a 修改a b不会跟着改变,有独立的内存


	//1.JSON.parse(JSON.stringify())
	// let c = {}
	// c = JSON.parse(JSON.stringify(a))
	// a.name  = '猪八戒'
	// a.car[0] = '奔跑'
	// console.log(a)
	// console.log(b)

	//2.递归函数
	let m = {
		name:'孙悟空',
		age : 20,
		car:['筋斗云','BWM'],
		dog:{
			name:'dog',
			age :'2',
			color:'黑'
		}
	}
/*	function fn(a,b){
		for(let key in a){
			let item = a[key]
			// console.log(item)
			// 数组
			if(item instanceof Array){
				console.log(item)
				b[key] = []
				fn(item,b[key])
			}else if(item instanceof Object){
				console.log(item,'========')
				b[key] = {}
				fn(item,b[key])
			}else{
				//基本数据类型直接赋值
				b[key] = item
		}
	}
}
	let n = {}
	fn(m,n)
	m.dog.color = '黄狗'
	console.log(m,n)*/

	// 3 jquery中的extend方法
	let n = {}
	$.extend(true,n,m)
	m.dog.color = '黄狗'
	console.log(m,n)
</script>
</body>
</html>